﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace PDX
{
    /// <summary>
    /// Helper methods for accessing the visual tree
    /// </summary>
    public static class VisualTree
    {
        /// <summary>
        /// Walks the visual tree upward from the given element
        /// </summary>
        /// <param name="obj">The element to start with.</param>
        /// <param name="includeSelf">if set to <c>true</c> the starting element will be included in the results</param>
        /// <returns></returns>
        public static IEnumerable<DependencyObject> WalkUp(this DependencyObject obj, bool includeSelf = false)
        {
            if (includeSelf)
            {
                yield return obj;
            }

            var parent = VisualTreeHelper.GetParent(obj);
            while (parent != null)
            {
                yield return parent;
                parent = VisualTreeHelper.GetParent(parent);
            }
        }
    }
}
